/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.uoc.tfc.exuoc.model;

/**
 *
 * @author emili
 */
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;

import edu.uoc.tfc.exuoc.config.Config;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Cascade;

import javax.persistence.Version;


@Entity
@Table(name="contenidors")
public class Contenidor implements Serializable, Comparable {

    private static final long serialVersionUID = 3245235352332L;
    private Long id=Config.valorNull;
    private String nom=Config.valorNullStr;
    private Date datapub=new Date();
    private int versionNum=Config.valorNullInt;

    // E-R Item
    //private Set<Item> items=new HashSet<Item>();
    private List<Item> items=new ArrayList<Item>(0);
    


    public Contenidor(){}

    public Contenidor(String _nom){

        this.emplenar(null, _nom, null);
        
        /*this.id=Config.valorNull;
        this.nom=_nom;
        this.dataPub = new java.util.Date();
         */
    }
    public Contenidor(Long _id,String _nom,Date _datapub){
        
       this.emplenar(_id, _nom, _datapub);
    }

      private void emplenar(Long _id,String _nom,Date _datapub){

           if (Long.valueOf(_id).equals(null)){  this.id=Config.valorNull;}
        else this.id=_id;

        if (_nom.equals(null)) this.nom=Config.valorNullStr;
        else this.nom=_nom;

        if (_datapub.equals(null)) this.datapub = new java.util.Date();
        else this.datapub=_datapub;
    }

    @Id
    @SequenceGenerator(name="contenidors", sequenceName="seqcontenidors")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="contenidors")
    public Long getId(){
        return this.id;
    }

    @Column(length=255,nullable=false,unique=true)
    public String getNom(){
        return this.nom;
    }

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="datapub",nullable=false)
    public Date getDatapub(){
        return this.datapub;
    }

    // Control d'exceptions :StaleObjectStateException
    @Version
    @Column(name="version", nullable=true)
    public int getVersionNum(){
        return this.versionNum;
    }

    @OneToMany(cascade=javax.persistence.CascadeType.ALL, mappedBy="contenidor")
    //Emili:11-01-2012, fetch=javax.persistence.FetchType.EAGER
    @OrderBy("nom ASC")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> _Items) {
        this.items = _Items;
    }

    public void addItem(InterfaceItem _item)
    {
        // si el tenim no cal afegir-lo
        if ( ! this.items.contains((Item)_item))
            this.items.add((Item)_item);
    }

    public void delItem(Item _item)
    {
        //Si exiteix el borrem
        if (this.items.contains(_item))
            this.items.remove(_item);
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }
    //No s'hauria de poder assignar un valor a data ja que ho fa la base de dades
    protected void setDatapub(Date _data){
        this.datapub=_data;
    }

    //17-11-2011 : abans l get i set eren protected
    public void setVersionNum(int versionNum) {
        this.versionNum = versionNum;
    }



    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Contenidor other = (Contenidor) obj;
        /* Emili:03-01-2012:if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
            return false;
        }
        return true;
         *
         */

        if (this.id !=other.id){
            return false;
        }
        if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 97 * hash + (this.nom != null ? this.nom.hashCode() : 0);
        hash = 97 * hash + (this.datapub != null ? this.datapub.hashCode() : 0);
        return hash;
    }

    @Override
      public int compareTo(Object o) {
       /* Emili: 30-01-2012: Contenidor i =null;
        if (o == null) {
            return -2;
        }
        if (getClass() != o.getClass()) {
           return -2;
        }
        i=(Contenidor)o;

        return this.id.compareTo(i.getId());
        *
        */
         Contenidor i =null;
        if (o == null) {
            return -2;
        }
        if (!this.getClass().equals( o.getClass())) {
           return -2;
        }
        i=(Contenidor)o;

        return (this.equals(i))? 0:1;

    }

    @Override
    public String toString(){
        String cad="";
        if (this.id != null ){
            cad=this.id+":";
        }

        if (this.nom != null ){
            cad=cad.concat(this.getNom()+":");
        }
        //cad+=this.getNom()+": ";

        if (this.datapub != null ){
            cad=cad.concat(this.getDatapub().toString());
        }
        //Emili:31-12-2011:cad=cad.concat(":Versio="+String.valueOf(this.versionNum));
        
        return cad;
    }


}
